package com.archerdu.apply.controller.admin;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.archerdu.apply.model.Role;
import com.archerdu.apply.model.Member;
import com.archerdu.apply.service.RoleManager;
import com.archerdu.apply.service.MemberManager;
import com.archerdu.base.controller.BaseController;
import com.archerdu.base.dao.IPage;
import com.archerdu.commons.constant.DataConstant;
import com.archerdu.commons.util.ServletUtil;

/**
 * 
 * @projectName  springDemo 
 * @description  会员类   
 * @author       archer du  
 * @createDate   2013年10月29日 下午1:35:51   
 * @updateMember   dxf   
 * @updateDate   2013年10月29日 下午1:35:51   
 * @updateRemark 新建 
 * @version      0.0.1
 */
@Controller
@Scope("prototype")
@RequestMapping("/admin/member")
public class MemberController extends BaseController {

    private static final long serialVersionUID = -6777950424131138768L;

    private MemberManager       memberManager;

    private RoleManager       roleManager;

    private Member              member;

    @RequestMapping(value = "/add")
    public ModelAndView add(HttpServletRequest requ, MemberController uc) {
        ModelAndView mav = new ModelAndView();
        member = uc.getMember();

        if ("post".equalsIgnoreCase(requ.getMethod())) {
            try {
                member.setFlag(DataConstant.USER_FLAG_SYS);
                member.setRegIp(ServletUtil.getClientIpAddr(requ));
                member.setRegtime(getSqlDate());
                member.setUseFlag(DataConstant.COMMON_USE_FLAG_YES);
                memberManager.add(member);
                addMessage(DataConstant.COMMON_ADD_SUCCESS);
            } catch (Exception e) {
                addMessage(DataConstant.COMMON_ADD_FAILED);
                logger.error(e.getMessage(), e);
            }
        }
        mav.addObject(getMessage());
        mav.setViewName("admin/member/add");
        return mav;
    }

    /**
     * 用户编辑
     * 
     * @param requ
     * @param uc
     * @return
     */
    @RequestMapping(value = "/edit")
    public ModelAndView edit(HttpServletRequest requ, MemberController uc) {
        logger.debug("uc.getMember() is " + uc.getMember());
        member = uc.getMember();
        ModelAndView mav = new ModelAndView();
        if ("get".equalsIgnoreCase(requ.getMethod())) {
            if (!StringUtils.isEmpty(member.getId())) {
                member = memberManager.get(member.getId());
                mav.addObject(member);
            }
        }

        mav.setViewName("admin/member/edit");
        return mav;
    }

    /**
     * 删除用户
     * 
     * @param requ
     * @param uc
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/remove")
    public ModelAndView remove(HttpServletRequest requ, MemberController uc, Integer pageNo, Integer pageSize) {
        member = uc.getMember();
        if (null != member && null != member.getId()) {
            member = memberManager.get(member.getId());
            member.setUseFlag(DataConstant.COMMON_USE_FLAG_NO);
            try {
                memberManager.modify(member);
                addMessage(DataConstant.COMMON_DEL_SUCCESS);
            } catch (Exception e) {
                addMessage(DataConstant.COMMON_DEL_FAILED);
                logger.error(e.getMessage(), e);
            }
        }

        ModelAndView mav = page(requ, uc, pageNo, pageSize);
        mav.addObject(getMessage());
        return mav;
    }

    /**
     * 用户分页查询
     * 
     * @param requ
     * @param uc
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping(value = "/page")
    public ModelAndView page(HttpServletRequest requ, MemberController uc, Integer pageNo, Integer pageSize) {

        if (null == uc.getMember()) {
            member = new Member();
        } else {
            member = uc.getMember();
        }
        member.setUseFlag(DataConstant.COMMON_USE_FLAG_YES);
        if (null == pageNo || null == pageSize) {
            pageNo = 1;
            pageSize = 20;
        }
        logger.debug("memberManager is " + memberManager);
        IPage<Member> page = memberManager.pageSearch(member, pageNo, pageSize);

        ModelAndView mav = new ModelAndView();
        mav.addObject(page).setViewName("admin/member/page");
        return mav;
    }

    @RequestMapping("/role")
    public ModelAndView role(HttpServletRequest request, MemberController uc, Integer[] roleIds) {

        member = uc.getMember();
        ModelAndView mav = new ModelAndView();
        List<Role> roleList = new ArrayList<Role>();

        logger.info("begin to add roles");
        if ("post".equalsIgnoreCase(request.getMethod()) && null != member && null != member.getId()) {
            member = memberManager.get(member.getId());

            Set<Role> roles = new HashSet<Role>();
            if (null != roleIds) {
                for (Integer i : roleIds) {
                    roles.add(roleManager.get(i));
                }
            }
            member.setRoles(roles);
            logger.info("begin to add roles");
            try {
                memberManager.modifyByMerge(member);
                this.addMessage(DataConstant.COMMON_ADD_SUCCESS);
                logger.info("success to add use's roles");

            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                this.addMessage(DataConstant.COMMON_ADD_FAILED);
                logger.info("failed to add use's roles");
            }
        }

        // 获取所有关联角色
        if (null != member && null != member.getId()) {
            member = memberManager.get(member.getId());
            roleList = roleManager.getAll();
            for (Role ro : roleList) {
                for (Role ro1 : member.getRoles()) {
                    if (ro.getId().equals(ro1.getId())) {
                        ro.setChecked(DataConstant.COMMON_HTML_CHECKED);
                        // struts2标签属性
                        // ro.setChecked("true");
                    }
                }
            }
            mav.addObject("roleList", roleList);
        }
        mav.addObject("member", member);
        mav.addObject(getMessage());
        return mav;
    }

    public void setMemberManager(MemberManager memberManager) {
        this.memberManager = memberManager;
    }

    public Member getMember() {
        return member;
    }

    public void setMember(Member member) {
        this.member = member;
    }

    public RoleManager getRoleManager() {
        return roleManager;
    }

    public void setRoleManager(RoleManager roleManager) {
        this.roleManager = roleManager;
    }

    public MemberManager getMemberManager() {
        return memberManager;
    }
}
